/**
 * Created by wengms on 2016/11/20.
 */
define([
    "angular",
    'framework/msg/GillionMsgModule',
    ],function(angular){

    return angular
        .module("PermissionModule",['GillionMsgModule'])
        .controller("PermissionController",function($scope,Resource,GillionMsg,GillionMsgService){
            //1.获取targetId和targetType
            var targetInfo = GillionMsgService.getInputData();

            //默认
            var UserPermissions = Resource("/grule-manager/permissions/:targetId/:targetType/:mode/users/:id",{
                "targetId":"@targetId",
                "targetType":"@targetType",
                "id":"@id",
                "mode":"@mode"
            },{
                get:{
                    isArray:true
                }
            });
            var GroupPermissions = Resource("/grule-manager/permissions/:targetId/:targetType/:mode/userGroups/:id",{
                "targetId":"@targetId",
                "targetType":"@targetType",
                "id":"@id",
                "mode":"@mode"
            },{
                get:{
                    isArray:true
                }
            });

            var refreshUserInfoPermissions = function(callback){
                $scope.selects=[];
                $scope.noSelects=[];
                UserPermissions.get(targetInfo,function(permissionInfos){
                    for(var i=0;i<permissionInfos.length;i++){
                        if(permissionInfos[i].id){
                            $scope.selects.push(permissionInfos[i]);
                        }else{
                            $scope.noSelects.push(permissionInfos[i]);
                        }
                    }
                    if (callback){
                        callback();
                    }
                });

            }

            var refreshGroupPermissions = function(callback){
                $scope.selectGroups=[];
                $scope.noSelectGroups=[];
                GroupPermissions.get(targetInfo,function(permissionInfos){
                    for(var i=0;i<permissionInfos.length;i++){
                        if(permissionInfos[i].id){
                            $scope.selectGroups.push(permissionInfos[i]);
                        }else{
                            $scope.noSelectGroups.push(permissionInfos[i]);
                        }
                    }
                    if (callback){
                        callback();
                    }
                });
            }
            refreshUserInfoPermissions();
            refreshGroupPermissions();



            /**
             * 用户权限操作
             * @param permissionInfo
             */
            $scope.userPermissionOperate = function(noSelect){
                var dlgWait= GillionMsg.wait('提示信息', '数据保存中...',false);
                var permissionData = {
                    id:noSelect.id,
                    targetId:targetInfo.targetId,
                    targetType:targetInfo.targetType,
                    ownerType:0,
                    ownerId:noSelect.userId,
                    mode:targetInfo.mode
                };
                //选中,并记录
                UserPermissions.create(permissionData,function(){
                    refreshUserInfoPermissions(function(){
                        dlgWait.close();
                    });
                });
            }
            $scope.deleteSelect= function (select) {
                var dlgWait= GillionMsg.wait('提示信息', '数据保存中...',false);
                var permissionData = {
                    id:select.id,
                    targetId:targetInfo.targetId,
                    targetType:targetInfo.targetType,
                    ownerType:0,
                    ownerId:select.userId,
                    mode:targetInfo.mode
                };
                    //取消选择,删除原有记录
                    UserPermissions.delete(permissionData,function(){
                        refreshUserInfoPermissions(function(){
                            dlgWait.close();
                        });
                    });
            }


            /**
             * 用户组权限操作
             */
            $scope.userGroupPermissionOperate = function(noSelectGroup){
                var dlgWait= GillionMsg.wait('提示信息', '数据保存中...',false);
                var permissionData = {
                    id:noSelectGroup.id,
                    targetId:targetInfo.targetId,
                    targetType:targetInfo.targetType,
                    ownerType:1,
                    ownerId:noSelectGroup.groupId,
                    mode:targetInfo.mode

                };
                    //选中,并记录
                    GroupPermissions.create(permissionData,function(){
                        refreshGroupPermissions(function(){
                            dlgWait.close();
                        });
                    });
                };
            $scope.deleteSelectGroup=function(selectGrouop){
                var dlgWait= GillionMsg.wait('提示信息', '数据保存中...',false);
                var permissionData = {
                    id:selectGrouop.id,
                    targetId:targetInfo.targetId,
                    targetType:targetInfo.targetType,
                    ownerType:1,
                    ownerId:selectGrouop.groupId,
                    mode:targetInfo.mode

                };
                //取消选择,删除原有记录
                GroupPermissions.delete(permissionData,function(){
                    refreshGroupPermissions(function(){
                        dlgWait.close();
                    });
                });
            }
        })

});
